#include "PauseState.h"

using namespace Ogre;

PauseState::PauseState()
:	m_bQuit(false)
,	m_bRestartGame(false)
,	m_bQuestionActive(false)
{
    m_FrameEvent        = Ogre::FrameEvent();
}

void PauseState::Enter()
{
	AppFramework::getSingletonPtr()->m_pLog->logMessage("Entering PauseState...");

	m_pSceneMgr = AppFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "PauseSceneMgr");
	m_pSceneMgr->addRenderQueueListener(AppFramework::getSingleton().m_pOverlaySystem);
	m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.7f, 0.7f, 0.7f));

    m_pCamera = m_pSceneMgr->createCamera("PauseCam");
    m_pCamera->setPosition(Vector3(0, 25, -50));
    m_pCamera->lookAt(Vector3(0, 0, 0));
    m_pCamera->setNearClipDistance(1);

	m_pCamera->setAspectRatio(Real(AppFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
		Real(AppFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

	AppFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

	AppFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
	AppFramework::getSingletonPtr()->m_pTrayMgr->showCursor();
	AppFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "BackToGameBtn", "Resume",		250);
	AppFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "RestartGame",	 "Restart",		250);
	AppFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "ExitBtn",		 "Exit",		250);
	AppFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_TOP,		"PauseLbl",		 "Game Pause",  250);

    m_bQuit = false;
	m_bRestartGame	= false;

    CreateScene();
}

void PauseState::CreateScene()
{
}

void PauseState::Exit()
{
	AppFramework::getSingletonPtr()->m_pLog->logMessage("Leaving PauseState...");

    m_pSceneMgr->destroyCamera(m_pCamera);
    if (m_pSceneMgr)
		AppFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);

	AppFramework::getSingletonPtr()->m_pTrayMgr->clearAllTrays();
	AppFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
	AppFramework::getSingletonPtr()->m_pTrayMgr->setListener(0);
}

bool PauseState::keyPressed(const OIS::KeyEvent &keyEventRef)
{
	if (AppFramework::getSingletonPtr()->m_inputContext.mKeyboard->isKeyDown(OIS::KC_ESCAPE) && !m_bQuestionActive)
    {
        m_bQuit = true;
        return true;
    }

	AppFramework::getSingletonPtr()->keyPressed(keyEventRef);

    return true;
}

bool PauseState::keyReleased(const OIS::KeyEvent &keyEventRef)
{
	AppFramework::getSingletonPtr()->keyReleased(keyEventRef);

    return true;
}

bool PauseState::mouseMoved(const OIS::MouseEvent &evt)
{
	if (AppFramework::getSingletonPtr()->m_pTrayMgr->injectMouseMove(evt)) return true;
    return true;
}

bool PauseState::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	if (AppFramework::getSingletonPtr()->m_pTrayMgr->injectMouseDown(evt, id)) return true;
    return true;
}

bool PauseState::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	if (AppFramework::getSingletonPtr()->m_pTrayMgr->injectMouseUp(evt, id)) return true;
    return true;
}

void PauseState::Update(double fDeltaTime)
{
	m_FrameEvent.timeSinceLastFrame = fDeltaTime;
	AppFramework::getSingletonPtr()->m_pTrayMgr->frameRenderingQueued(m_FrameEvent);

	if (m_bRestartGame)
	{
		popAppState();
		AppState* pGameState = findByName("GameState");
		if (pGameState)
			pGameState->Restart();
		return;
	}

	if (m_bQuit == true)
	{
		popAppState();
		return;
	}
}

void PauseState::buttonHit(OgreBites::Button *button)
{
	if (button->getName() == "ExitBtn")
	{
		AppFramework::getSingletonPtr()->m_pTrayMgr->showYesNoDialog("Sure?", "Really leave?");
		m_bQuestionActive = true;
	}
	else if(button->getName() == "BackToGameBtn")
		m_bQuit = true;
	else if(button->getName() == "BackToMenuBtn")
		popAllAndPushAppState(findByName("MenuState"));
	else if (button->getName() == "RestartGame")
		m_bRestartGame = true;
}

void PauseState::yesNoDialogClosed(const Ogre::DisplayString& question, bool yesHit)
{
    if (yesHit == true)
        shutdown();
    else
		AppFramework::getSingletonPtr()->m_pTrayMgr->closeDialog();

    m_bQuestionActive = false;
}
